﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Kotonoha.Server.Dictionaries;
using Kotonoha.Server.Dictionaries.JMDict;
using Kotonoha.Server.Examples;
using Kotonoha.Server.Plugins;
using Kotonoha.Server.Search;
using Kotonoha.Server.Utils;
using log4net;
using log4net.Config;
using NHibernate;
using NHibernate.Cfg;

namespace Kotonoha.Server.Tests {
    internal static class RunClass {
        private static readonly ILog log = LogManager.GetLogger(typeof (RunClass));

        private static void Main2(string[] args) {
            XmlConfigurator.Configure(new FileInfo(@"log4net.config.xml"));
            Configuration cfg = new Configuration();
            cfg.Configure();
            cfg.SetProperty("connection.connection_string", @"Data Source=e:\temp\db.db");
            cfg.AddAssembly("Kotonoha");

            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            SQLiteConnection connection = new SQLiteConnection(@"Data Source=e:\temp\db.db");
            connection.Open();
            SQLiteCommand c = new SQLiteCommand(connection);
            c.CommandText = "PRAGMA synchronous = OFF; ";
            c.ExecuteScalar();
            c.CommandText = "PRAGMA temp_store = MEMORY;";
            c.ExecuteScalar();
            connection.Close();

            session.CreateSQLQuery("CREATE INDEX IDX ON SEARCH_SUBSTRING (VALUE)").ExecuteUpdate();
            transaction.Commit();
            transaction = session.BeginTransaction();

            PluginManager.Instance.Load(new DirectoryInfo("."));
            /*ResourceManager.Instance.Configure();
            try {
                IEnumerable<WordEntry> entries = ResourceManager.Instance.PresentWords.Take(25000);
                foreach (var entry in entries) {
                    session.Save(entry);
                }
                transaction.Commit();
                new Muncher<WordEntry, WordReadingPosition>(factory.OpenStatelessSession(), e => e.Readings, 1000).Munch
                    (entries);
            } catch (Exception e) {
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            } finally {
                //transaction.Commit();
                session.Close();
                factory.Close();
            }*/
        }

        private static void Main(string[] args) {
            XmlConfigurator.Configure(new FileInfo(@"log4net.config.xml"));
            Configuration cfg = new Configuration();
            cfg.Configure();
            cfg.SetProperty("connection.connection_string", @"Data Source=e:\temp\db.db");
            cfg.AddAssembly("Kotonoha");

            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            SQLiteConnection connection = new SQLiteConnection(@"Data Source=e:\temp\db.db");
            connection.Open();
            SQLiteCommand c = new SQLiteCommand(connection);
            c.CommandText = "PRAGMA synchronous = OFF; ";
            c.ExecuteScalar();
            c.CommandText = "PRAGMA temp_store = MEMORY;";
            c.ExecuteScalar();
            connection.Close();

            session.CreateSQLQuery("CREATE INDEX IDX ON SEARCH_SUBSTRING (VALUE)").ExecuteUpdate();
            transaction.Commit();
            transaction = session.BeginTransaction();

            PluginManager.Instance.Load(new DirectoryInfo("."));
            /*ResourceManager.Instance.Configure();
            try {
                var entries = ResourceManager.Instance.PresentWords;
                foreach (var entry in entries) {
                    session.Save(entry);
                }
                transaction.Commit();
                log.Debug("Saved words");
                using (IStatelessSession statelessSession = factory.OpenStatelessSession()) {
                    transaction = statelessSession.BeginTransaction();
                    saveExamples(entries, statelessSession);
                    transaction.Commit();
                }
            } catch (Exception e) {
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            } finally {
                //transaction.Commit();
                session.Close();
                factory.Close();
            }*/
        }

        public static void saveExamples(IWordDictionary dic, IStatelessSession session) {
            FileStream stream = File.OpenRead(@"e:\Temp\wap_soft\examples");
            foreach (var i in ExampleFileReader.read(stream, Encoding.GetEncoding("euc-jp"), null)) {
                ExampleEntry example = Examples.Examples.createExample(i.Example);
                session.Insert(example);

                HashSet<WordEntry> done = new HashSet<WordEntry>();
                foreach (Word w in i.Positions) {
                    IEnumerable<WordEntry> enumerable = dic.ByWriting(w.Content);
                    foreach (WordEntry entry in enumerable) {
                        if (done.Contains(entry)) {
                            continue;
                        }
                        done.Add(entry);
                        var data = new ExampleInfo();
                        data.Example = example;
                        data.Number = w.Meaning;
                        data.Recommended = w.Recommended ? 1 : 0;
                        data.Word = entry;
                        session.Insert(data);
                    }
                }
            }
        }
    }
}
